﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace WFIM.Framework.Collections
{
    public class SafeDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public event EventHandler<SafeDictionaryEventargs> Added ;
        public event EventHandler<SafeDictionaryEventargs> OnRemove ;
        public event EventHandler<SafeDictionaryEventargs> Removed ;

        public new void Add(TKey key,TValue value)
        {
            lock (this)
            {
                base.Add(key,value);

                if (Added != null)
                    Added(this, new SafeDictionaryEventargs(value));
            }
        }


        public new void Remove(TKey key)
        {
            lock (this)
            {
                var item = this[key];

                if (OnRemove != null)
                    OnRemove(this, new SafeDictionaryEventargs(item));

                base.Remove(key);

                if (Removed != null)
                    Removed(this, new SafeDictionaryEventargs(item));
            }
        }

        public class SafeDictionaryEventargs : EventArgs
        {
            public Object Data { get; set; }

            public SafeDictionaryEventargs(object args)
            {
                Data = args;
            }
        }
    }
}
